﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxPro;
using System.Data.SqlClient;

public partial class pageset : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.TextBox tbSearch;
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.RegisterTypeForAjax(typeof(pageset));
        if (!IsPostBack)
        {          
            binddata();
        }
    }

    //[AjaxMethod()]
    public ArrayList GetSearchItems(string query)
    {
        ArrayList items = new ArrayList();
        string myConnectionString =
            "server=HOME-D340431992\\SQLEXPRESS;database=medical; uid=sa;pwd=123";
        string mySelectQuery =
            string.Format(
            "select name from PackageName where CHARINDEX('{0}',LOWER(input_code)) = 1",
            query);
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
        myConnection.Open();
        SqlDataReader myReader =
            myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        while (myReader.Read())
        {
            items.Add(myReader.GetString(0));
        }
        myReader.Close();
        return items;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (testid() == 0)
        {
            string str_uid = getid();
            string str = "INSERT INTO usedpackage (unit_id,package_id) VALUES ( '" + str_uid + "' ,'" + search.Value.ToString() + "');";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["medical"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('包" + search.Value + "成功添加')</script>");
            this.Response.Write("<script>window.location.href=window.location.href;</script>");
        }
        else
        {
            Response.Write("<script>alert('包已经存在')</script>");
        }
    }
    protected int testid()
    {
        int flag=0;
        string str = "SELECT package_id FROM usedpackage;";
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["medical"].ConnectionString);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(str, con);
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        sda.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i][0].ToString() == search.Value.ToString())
            {
                flag = 1;
                break;
            }
        }
        return flag;
    }
    protected string getid()
    {
        string str = "select unit_id from opeartorman where no='" + Session["userid"].ToString() + "';";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["medical"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand(str, con);
        string str_id = (cmd.ExecuteScalar()).ToString();
        con.Close();
        return str_id;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        search.Value = "";
    }
    protected void binddata()
    {
        string str_id = Session["userid"].ToString();
        sqldata sd = new sqldata();
        string str = "SELECT package_id FROM usedpackage,opeartorman where usedpackage.unit_id=opeartorman.unit_id and opeartorman.no='" + str_id + "';";
        sd.dropdownlistdatabind(str, DropDownList1);
        ListItem nullList = new ListItem("", null);
        DropDownList1.Items.Insert(0, nullList);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        
        string str = "DELETE FROM usedpackage WHERE package_id ='" + DropDownList1.Text.ToString() + "' and unit_id='"+getid()+"';";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["medical"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script languge='javascript'>alert('删除成功!'); window.location.href='pageset.aspx'</script>");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        DropDownList1.SelectedIndex = 0;
    }
}
